/*
 * 查找高亮词条
 * string:key 需要匹配的关键字
 */
function SearchHighlight(key) {
    $.ajax({
        url: '../data/listRequestCategroy.json',
        dataType: 'json'
    })
    .done(function(data) {
        console.log("查找 成功");
        console.log(data);
        if (data.length !== 0) {
            var par, chi, fir, sec, thi, iii, jjj, num = 0,
                $el = $("#result_search_list");
            par = '<dt class="searchNum">搜索“<span class="searchWord">' + key + '</span>”，共找到<span class="num">~!@#$%^&*</span>个服务</dt>'
            for (fir in data) {
                console.log("fir " + fir);
                for (iii = 0; iii < data[fir].length; iii++) {
                    // console.log("iii " + iii);
                    for (sec in data[fir][iii]) {
                        console.log("sec " + sec);
                        chi = "";
                        for (jjj = 0; jjj < data[fir][iii][sec].length; jjj++) {
                            // console.log("jjj " + jjj);
                            for (thi in data[fir][iii][sec][jjj]) {
                                console.log("thi " + thi);
                                if (thi.split(key).length > 1) {
                                    // 查找到数据
                                    if (data[fir][iii][sec][jjj][thi].indexOf("http") > -1 ) {
                                        chi += '<li><b>&lowast;&ensp;</b><a href=' + data[fir][iii][sec][jjj][thi] + ' target="_blank">' + thi.replace(key, '<i>' + key + '</i>') + '</a></li>';
                                        num++;
                                        console.log(thi.replace(key, "<i>" + key + "</i>") + " " + num);
                                    } else {
                                        chi += '<li><b>&lowast;&ensp;</b><a href="detail.html?type=' + data[fir][iii][sec][jjj][thi] + '&tier=' + encodeURI(fir + '$' + sec + '$' + thi) + '">' + thi.replace(key, '<i>' + key + '</i>') + '</a></li>';
                                        num++;
                                        console.log(thi.replace(key, "<i>" + key + "</i>") + " " + num);
                                    }
                                }
                            }
                        }
                        if (chi !== "") {
                            par += '<dd class="Items_resultBox"><h5>' + sec.replace(key, "<i>" + key + "</i>") + '</h5><ul class="clearfix">' + chi + '</ul></dd>';
                        }
                    }
                }
            }
            $el.html(par.replace("~!@#$%^&*", "<i>" + num + "</i>"));
        }
    })
    .fail(function() {
        console.log("查找 失败");
    })
    .always(function() {
        console.log("查找 完成");
    });
}

// 调用数据接口
+function() {
    $("#result_search_list").empty();
    if (GetUrlParam("keyword")) {
        var word = GetUrlParam("keyword");
        $('#result_search_input').val(word);
        SearchHighlight(word);
    }
}();

// 事件绑定
-function() {
    // 搜索框预留字
    // inputFn($('.result .searchB'));
    $(".result .searchBox .searchB").focus();
    $(".result .searchBox .bar").on('click', '.submitB', function(event) {
        $("#result_search_list").empty();
        SearchHighlight($('.result .searchB').val().trim());
    });
    $(".result .searchBox .bar").on('keydown', '.searchB', function(event) {
        console.log(event.keyCode);
        if (event.keyCode === 13 && $(this).val().trim() !== "") {
            SearchHighlight($(this).val().trim());
        }
    });
}();